package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 统计指定时间区域的营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        if(begin.isAfter(end)){
            throw new RuntimeException("传输时间区域错误");
        }
        //创建一个日期集合，来将begin-end区间内的每一天加入集合里面
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>();
        //构建turnoverList
        for (LocalDate date : dateList) {
            //先准备sql语句中需要的时间区域
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer status = Orders.COMPLETED;
            //利用这两个时间查询order表
            Map map = new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            map.put("status",status);
            Double turnover = orderMapper.turnoverStatistics(map);
            turnover = (turnover == null)? 0.0:turnover;
            turnoverList.add(turnover);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    /**
     * 统计指定时间区域内用户统计功能
     * 分为用户总量和新增用户数量
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //日期，以逗号分隔，例如：2022-10-01,2022-10-02,2022-10-03
        //    private String dateList;
        //
        //    //用户总量，以逗号分隔，例如：200,210,220
        //    private String totalUserList;
        //
        //    //新增用户，以逗号分隔，例如：20,21,10
        //    private String newUserList;
        if(begin.isAfter(end)){
            throw new RuntimeException("传输时间区域错误");
        }
        //创建一个日期集合，来将begin-end区间内的每一天加入集合里面
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> newUserList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();
        //统计每一天的新增数量，可以利用User中的createTime来进行判断
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //先进行新用户的判断
            Map map1 = new HashMap();
            map1.put("endTime",endTime);
            Integer totalUserNum = userMapper.getUserStatistics(map1);
            map1.put("beginTime",beginTime);
            Integer newUserNum = userMapper.getUserStatistics(map1);

            newUserList.add(newUserNum);
            totalUserList.add(totalUserNum);
        }
        return UserReportVO.builder()
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .dateList(StringUtils.join(dateList,","))
                .build();
    }

    /**
     * 订单统计
     * 业务规则：有效订单：状态为已完成
     * 展示订单总数和有效订单数
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        /**、
         * //日期，以逗号分隔，例如：2022-10-01,2022-10-02,2022-10-03
         *     private String dateList;
         *
         *     //每日订单数，以逗号分隔，例如：260,210,215
         *     private String orderCountList;
         *
         *     //每日有效订单数，以逗号分隔，例如：20,21,10
         *     private String validOrderCountList;
         *
         *     //订单总数
         *     private Integer totalOrderCount;
         *
         *     //有效订单数
         *     private Integer validOrderCount;
         *
         *     //订单完成率
         *     private Double orderCompletionRate;
         */
        if(begin.isAfter(end)){
            throw new RuntimeException("传输时间区域错误");
        }
        //创建一个日期集合，来将begin-end区间内的每一天加入集合里面
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> validOrderList = new ArrayList<>();
        List<Integer> OrderList = new ArrayList<>();

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer status = Orders.COMPLETED;
            //利用这两个时间查询order表
            Map map = new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            Integer OrderNum = orderMapper.getOrderNum(map);
            map.put("status",status);
            Integer validOrderNum = orderMapper.getOrderNum(map);
            OrderList.add(OrderNum);
            validOrderList.add(validOrderNum);
        }

        Integer totalOrderCount =  OrderList.stream().mapToInt(Integer::intValue).sum();
        Integer validOrderCount =  validOrderList.stream().mapToInt(Integer::intValue).sum();
        Double orderCompletionRate =  0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue()/ totalOrderCount;
        }


        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .validOrderCountList(StringUtils.join(validOrderList,","))
                .orderCountList(StringUtils.join(OrderList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 统计指定时间内销量前10菜品
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO top10Statistics(LocalDate begin, LocalDate end) {
        ////商品名称列表，以逗号分隔，例如：鱼香肉丝,宫保鸡丁,水煮鱼
        //    private String nameList;
        //
        //    //销量列表，以逗号分隔，例如：260,215,200
        //    private String numberList;
        LocalDateTime beginTime = LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> top10 = orderMapper.getSalesTop10(beginTime, endTime);
        List<String> nameList = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        for (GoodsSalesDTO goodsSalesDTO : top10) {
            nameList.add(goodsSalesDTO.getName());
            numberList.add(goodsSalesDTO.getNumber());
        }
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }
}
